package net.expectx.gw.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.expectx.common.base.BaseResult;
import net.expectx.common.util.Md5Util;
import net.expectx.gw.bean.PermissionUser;
import net.expectx.gw.service.PermissionUserService;
import net.expectx.gw.util.AdminConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * controller
 * @author lijian
 * @date 2019/1/18
 */
@Controller
@RequestMapping("/manager/permission/user")
@Api(value = "控制器", description = "管理")
public class PermissionUserController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(PermissionUserController.class);


    @Autowired
    private PermissionUserService permissionUserService;
    @ApiOperation(value = "用户首页")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "permissionUser/index";
    }

    @ApiOperation(value = "个人资料")
    @RequestMapping(value = "/personData/{id}", method = RequestMethod.GET)
    public String personData(@PathVariable("id") Long id, ModelMap modelMap){
        PermissionUser permissionUser = permissionUserService.selectByPrimaryKey(id);
        modelMap.put("permissionUser", permissionUser);
        return "permissionUser/personInfo";
    }
    @ApiOperation(value = "修改密码")
    @RequestMapping(value = "/alterPassword", method = RequestMethod.GET)
    public String alterPassword() {
        return "permissionUser/modifyPassword";
    }
    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        return "permissionUser/modify";
    }
    @ApiOperation(value = "修改用户")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") Long id, ModelMap modelMap) {
        PermissionUser permissionUser = permissionUserService.selectByPrimaryKey(id);
        modelMap.put("permissionUserTo", permissionUser);
        return "permissionUser/modify";
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "1", value = "page") int page,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit
            ,String userName) {
        String strWhere="1=1 and is_deleted=0 ";
        String strOrder=null;
        if (StringUtils.isNotBlank(userName)) {
            strWhere+=" and user_name like('%" + userName + "%')";
        }
        List<PermissionUser> permissionSystems = permissionUserService.findByPage(page,limit,strWhere,strOrder);
        long total = permissionUserService.count(strWhere);
        Map<String, Object> result = new HashMap<>(16);
        result.put("code", AdminConstants.LAYUI_DATA_OK);
        result.put("count", total);
        result.put("msg", "");
        result.put("data", permissionSystems);
        return result;
    }
    @ApiOperation(value = "新增用户")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Object create(PermissionUser permissionUser) {
        permissionUser.setGmtCreate(new Date());
        permissionUser.setGmtModified(new Date());
        permissionUser.setIsDeleted(AdminConstants.NO);
        permissionUser.setSalt(UUID.randomUUID().toString().toLowerCase().replace("-",""));
        permissionUser.setPassword(Md5Util.md5(permissionUser.getPassword()+permissionUser.getSalt()));

        return  permissionUserService.insertSelective(permissionUser);
    }
    @ApiOperation(value = "修改密码验证")
    @RequestMapping(value = "/verify/{id}", method = RequestMethod.GET)
    @ResponseBody
    public boolean verify(@PathVariable("id") String id,String oldPassword) {
        boolean flag=false;
        PermissionUser permissionUser=permissionUserService.selectByPrimaryKey(id);
        if(permissionUser.getPassword().equals(Md5Util.md5(oldPassword+permissionUser.getSalt()))){
            flag=true;
        }
        return flag;
    }
    @ApiOperation(value = "修改用户")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update( PermissionUser permissionUser) {
        permissionUser.setGmtModified(new Date());
        if (permissionUser.getPassword() != null) {
            permissionUser.setPassword(Md5Util.md5(permissionUser.getPassword()+permissionUser.getSalt()));
        }
        BaseResult resultData = permissionUserService.updateByPrimaryKeySelective(permissionUser);
        PermissionUser userNew = permissionUserService.selectByPrimaryKey(permissionUser.getId());
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        session.setAttribute("permissionUser",userNew);
        return resultData;
    }
    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
        Map<String, Object> map = new HashMap<>(2);
        int count = permissionUserService.logicDeletion(AdminConstants.YES, "id in(" + ids + ")");
        if (count > 0) {
            count = permissionUserService.logicDeletion(AdminConstants.YES, "id in(" + ids + ")");
            map.put("flag", count > 0 ? true : false);
        } else {
            map.put("flag", count > 0 ? true : false);
        }
        return map;
    }

}
